﻿/// <reference path="../scripts/jquery-1.11.2.min.js" />
/// <reference path="../scripts/bootstrap.min.js" />

if (typeof ($) != "undefined" && $) {
    //验证用户
    if ($.cookie('openid') == null || $.cookie('openid') == '') {
        $.cookie('url', jQuery.url.attr("pageurl"));
        window.location.href = 'default.html';
    }
    else {
        $(document).ready(onReady);
    }
}

function onReady() {
    $("#btnSendCode").click(function () {
        var mobile = $("#mobile").val();
        if (CheckMobile(mobile)) {
            sendCode(mobile);
        }
        else {
            showToast("请输入正确的手机号");
        }
    });

    $("#btnRegister").click(function () {
        var mobile = $("#mobile").val();
        var code = $("#code").val();
        var referrer = $("#referrer").val();
        if (!CheckMobile(mobile)) {
            showToast("请输入手机号");
        }
        else if (code == '') {
            showToast("请输入验证码");
        }
        else {
            var data = { mobile: mobile, code: code, openid: $.cookie('openid'), referrer: referrer };
            $.ajax({
                type: "post",
                url: "api/login.ashx?ac=reg&k=" + Math.random(),
                data: data,
                beforeSend: function () {
                    $('#loadingToast').show();
                },
                error: function () {
                    showToast("用户注册失败");
                },
                success: function (data) {
                    var obj = eval("(" + data + ")").result;
                    if (obj.r == "suc") {
                        showToast("注册成功");
                        window.location.href = 'index.html';
                    }
                    else {
                        showToast(obj.val);
                    }
                },
                complete: function () {
                    $('#loadingToast').hide();
                }
            });
        }
    });
}

var InterValObj; //timer变量，控制时间
var count = 60; //间隔函数，1秒执行
var curCount; //当前剩余秒数
function sendCode(phone) {
    curCount = count;
    //设置button效果，开始计时
    $("#btnSendCode").attr("disabled", "disabled");
    $("#btnSendCode").html(curCount + "秒后重发");
    InterValObj = window.setInterval(SetRemainTime, 1000); //启动计时器，1秒执行一次
    //向后台发送处理数据
    var data = { mobile: phone };
    $.ajax({
        type: "post",
        url: "api/login.ashx?ac=sms&k=" + Math.random(),
        data: data,
        error: function () {
            showToast("发送失败");
        },
        success: function (data) {
            var obj = eval("(" + data + ")").result;
            if (obj.r == "suc") {
                showToast(obj.val);
            }
            else {
                showToast(obj.val);
            }
        }
    });
}

//timer处理函数
function SetRemainTime() {
    if (curCount == 0) {
        window.clearInterval(InterValObj); //停止计时器
        $("#btnSendCode").removeAttr("disabled"); //启用按钮
        $("#btnSendCode").html("发送验证码");
    }
    else {
        curCount--;
        $("#btnSendCode").html(curCount + "秒后重发");
    }
}

function CheckMobile(mobile) {
    var myreg = /^\d{11}$/;
    if (myreg.test(mobile)) {
        return true;
    }
    else {
        return false;
    }
}